<?php
header('Content-Type: text/html; charset=utf-8');
?>

<link rel="stylesheet" type="text/css" href="js/jquery.wysiwyg.css" />
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="js/jquery.wysiwyg.js"></script>
<link rel="stylesheet" type="text/css" href="css/post-editor.css">

<script type="text/javascript">
    $(function()
    {
        $('#wysiwyg').wysiwyg();
    });
</script>


<form name ="postForm"style="margin-left: 30px" onsubmit="return validateForm();" method="post" action="post.php">

    <p><strong><label for="title">Tiêu đề:</label></strong> <input type="text" value ="{post_title}" id ="title"name="title" name="title" size="80" /></p>
    <p> <lable id="error"></lable></p>
<p> {category_combo_box}</p>

<p><textarea name="wysiwyg" id="wysiwyg" rows="25" cols="70"> {post_content} </textarea></p>
<p><input type="submit" name="submit" id="submit" value="Lưu & Xuất bản">
    <input type="submit" name="savedraft" id="savedraft" value="Lưu nháp">
    <input type="hidden" name="postid" id="postid" value="{post_id}">
</p>

<script type='text/javascript'>

    function validateForm(){
        // Make quick references to our fields
        var title = document.getElementById('title');
        var content = document.getElementById('wysiwyg');	
        // Check each input in the order that it appears in the form!
        if(isEmpty(title, "*Chưa nhập tiêu đề") || 
            isEmpty(content,"*Chưa nhập nội dung")||
            !valid_combox(document.postForm.choose_category, document.postForm.choice_new_category)){
            return false;
        }
        return true;
	
    }
    function last_choice(selection) {
        return selection.selectedIndex == selection.length - 1; }
    
    function valid_combox(menu,txt) {
        if(menu.selectedIndex == 0) {
            alert('Vui lòng chọn chuyên mục cho bài viết');
            return false;} 
        if(txt.value == '') {
            if(last_choice(menu)) {
                alert('Vui lòng gõ tên chuyên mục mới');
                return false; }
            else {
                return true; }}
        else {
            if(!last_choice(menu)) {
                alert('Lựa chọn không hợp lệ');
                return false; }
            else {
                return true; }}}
    
    function activate(field) {
        field.disabled=false;
        if(document.styleSheets)field.style.visibility  = 'visible';
        field.focus(); }
    
    function process_choice(selection,textfield) {
        if(last_choice(selection)) {
            activate(textfield); }
        else {
            textfield.disabled = true;    
            if(document.styleSheets)textfield.style.visibility  = 'hidden';
            textfield.value = ''; }}
    
    function check_choice() {
        if(!last_choice(document.postForm.choose_category)) {
            document.postForm.choice_new_category.blur();
            alert('Please check your menu selection first');
            document.demoform.menu.focus(); }}

    function isEmpty(elem, helperMsg){
        if(elem.value.length == 0){
            var errorLable = document.getElementById('error');
            errorLable.innerHTML = helperMsg;
            errorLable.style.fontWeight = "Bold";
            errorLable.style.color = "Red";
            errorLable.style.fontFamily="arial,sans-serif";
            errorLable.style.fontSize = "15px";
            
            //alert(helperMsg);
            elem.focus(); // set the focus to this input
            return true;
        }
        return false;
    }

</script>
</form>
